<template>
	<view class="add">
		<u-field v-model="dataForm.name" label="规格名称" placeholder="请填写规格名称" input-align='right'></u-field>
		<u-field v-model="dataForm.amount" label="价格" placeholder="请填写价格" input-align='right'></u-field>
		<u-field v-model="dataForm.valorize_price" label="规定卖价" placeholder="请填写规定卖价" input-align='right'></u-field>
		<u-field v-model="dataForm.procurement_price" label-width='170' label="供应商价格" placeholder="请填写供应商价格"
			input-align='right'></u-field>
		<u-field v-model="dataForm.platfor_price" label="结算价格" placeholder="请填写结算价格" input-align='right'></u-field>
		<u-field v-model="dataForm.stock" label="库存数量" placeholder="请填写库存数量" input-align='right'></u-field>
		<u-field v-model="dataForm.rebate" label="订单返佣" placeholder="请填写订单返佣" input-align='right'></u-field>
		<u-field v-model="mobile" label-width='170' label="用户采购限制" disabled>
			<u-number-box slot='right' v-model="dataForm.max_num"></u-number-box>
		</u-field>
		<u-field v-model="mobile" label="排序" disabled>
			<u-number-box slot='right' v-model="dataForm.sort"></u-number-box>
		</u-field>
		<u-field v-model="mobile" label="是否上架" disabled>
			<view class="switch" slot='right'>
				<text>否</text>
				<u-switch v-model="upchecked" @change="upchange" size="30"></u-switch>
				<text>是</text>
			</view>
		</u-field>
		<u-field v-model="mobile" label="出行日期" disabled>
			<view class="switch" slot='right'>
				<text>不限</text>
				<u-switch v-model="timechecked" @change="datechange" size="30"></u-switch>
				<text>需要</text>
			</view>
		</u-field>
		<view class="" v-if="timechecked&&!dataForm.pk_id">
			<u-field v-model="form.start_date" label="开始时间" disabled input-align='right' @click="openstar">
				<u-icon slot='right' name="arrow-right"></u-icon>
			</u-field>
			<u-field v-model="form.end_date" label="结束时间" disabled input-align='right' @click="openend">
				<u-icon slot='right' name="arrow-right"></u-icon>
			</u-field>
			<u-field v-model="weekName" label="售卖日期" disabled input-align='right' @click="opendate">
				<u-icon slot='right' name="arrow-right"></u-icon>
			</u-field>
		</view>
		<view class="" v-if="timechecked&&dataForm.pk_id">
			<u-field v-model="date_travel" label="出行日期" disabled input-align='right' @click="openstardate">
				<u-icon slot='right' name="arrow-right"></u-icon>
			</u-field>
		</view>
		<view class="add-btn" @click="submit">{{type?'修改':'添加'}}</view>
		<u-picker mode="time" v-model="stimeshow" @confirm="changestar"></u-picker>
		<u-picker mode="time" v-model="etimeshow" @confirm="changeend"></u-picker>
		<u-picker mode="time" v-model="gotimeshow" @confirm="changego"></u-picker>
		<u-popup v-model="show" mode='bottom'>
			<view class="prop-box">
				<view class="box-list">
					<u-checkbox-group @change="checkboxGroupChange">
						<view class="box-item" v-for="(item, index) in weekList" :key="index">
							<u-checkbox v-model="item.checked" :name="item.id">{{item.value}}</u-checkbox>
						</view>
					</u-checkbox-group>
				</view>
				<view class="prop-btn" @click="save">保存</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		uniModal,
		uniNavigate,
		uniToast,
		uniBack
	} from '@/utils/uniPromise.js'
	import {
		formatDate,
		getDateObject
	} from '@/utils/moment/index.js'
	export default {
		data() {
			return {
				type:false,
				timechecked: false,
				stimeshow: false,
				etimeshow: false,
				upchecked: false,
				gotimeshow: false,
				show: false,
				weekName: '',
				dataForm: {
					fk_ticket_pro_id: '',
					name: '',
					amount: '',
					valorize_price: '',
					max_num: 0,
					procurement_price: '',
					platfor_price: '',
					rebate: '',
					stock: '',
					sort: 0,
					status: 1,
					travel_status: 1,
				},
				form: {
					start_date: '',
					end_date: '',
					day_week: '',
				},
				date_travel: '',
				selectweek: [],
				weekList: [{
						value: '星期一',
						id: 0,
						checked: false
					},
					{
						value: '星期二',
						id: 1,
						checked: false
					},
					{
						value: '星期三',
						id: 2,
						checked: false
					},
					{
						value: '星期四',
						id: 3,
						checked: false
					},
					{
						value: '星期五',
						id: 4,
						checked: false
					},
					{
						value: '星期六',
						id: 5,
						checked: false
					}, {
						value: '星期日',
						id: 6,
						checked: false
					}
				]
			}
		},
		onLoad(option) {
			// console.log(option)
			if (option.edit) {
				this.type = true
				let page = getCurrentPages()
				this.dataForm = page[page.length - 2].$vm.dataList[option.edit]
				this.upchecked = this.dataForm.status == 1?true:false
				this.timechecked = this.dataForm.travel_status == 1?false:true
				this.date_travel = formatDate(this.dataForm.date_travel, 'YYYY-MM-DD', 'YYYY-MM-DD HH:mm:ss')
				return
			}
			this.dataForm.fk_ticket_pro_id = option.fk_ticket_pro_id
		},
		methods: {
			checkboxGroupChange(val) {
				this.selectweek = val
			},
			save() {
				let arr = this.selectweek.map(item => {
					return this.weekList[item].value
				})
				this.weekName = arr.join(',')
				this.show = false
			},
			openstar() {
				this.stimeshow = true
			},
			openstardate() {
				this.gotimeshow = true
			},
			openend() {
				this.etimeshow = true
			},
			opendate() {
				this.show = true
			},
			changestar(val) {
				this.form.start_date = val.year + '-' + val.month + '-' + val.day
			},
			changeend(val) {
				this.form.end_date = val.year + '-' + val.month + '-' + val.day
			},
			changego(val) {
				this.date_travel = val.year + '-' + val.month + '-' + val.day
			},
			upchange(val) {
				this.dataForm.status = val ? 1 : 0
			},
			datechange(val) {
				this.dataForm.travel_status = val ? 0 : 1
				
				if(!val){
					this.date_travel = ''
					this.dataForm.date_travel = ''
					this.form.day_week = ''
					this.form.start_date = ''
					this.form.end_date = ''
				}
			},
			submit() {
				let arr = this.timechecked&&!this.dataForm.pk_id ? this.form : {}
				let params = {
					...this.dataForm,
					...arr
				}
				if (this.timechecked) {
					this.form.day_week = this.selectweek.join(',')
				}
				if (this.dataForm.pk_id) {
					params.date_travel = this.date_travel
					this.$UpdateModify('b_ticket_stock', {
						...params
					}).then(res => {
						uniToast('修改成功')
						setTimeout(() => {
							uniBack(1)
						}, 1000)
					})
				} else {
					this.$adminPost('AddTicketStock', {
						...params
					}).then(res => {
						uniToast('添加成功')
						setTimeout(() => {
							uniBack(1)
						}, 1000)

					})
				}

				// console.log(params)

			}
		}
	}
</script>
<style>
	page {
		background-color: #F5F5F5;
	}
</style>
<style lang="scss" scoped>
	.add {
		background-color: #fff;

		.add-btn {
			width: 690rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			color: #fff;
			background-color: #3297EF;
			border-radius: 10rpx;
			margin: 60rpx auto;
		}

		.switch {
			display: flex;
			align-items: center;
		}

		.box-list {
			min-height: 400rpx;
			padding: 30rpx;
			display: flex;
			// align-items: center;
			justify-content: space-between;
		}

		/deep/.u-checkbox-group {
			width: 750rpx !important;
		}

		/deep/.u-checkbox {
			width: 50% !important;
		}

		.prop-btn {
			width: 690rpx;
			line-height: 80rpx;
			height: 80rpx;
			text-align: center;
			color: #fff;
			background-color: #3297EF;
			border-radius: 10rpx;
			margin: 0 auto;
			position: relative;
			bottom: 100rpx;
		}
	}
</style>
